package com.snail.config;

import org.springframework.ai.model.SimpleApiKey;
import org.springframework.ai.openai.OpenAiAudioTranscriptionModel;
import org.springframework.ai.openai.OpenAiAudioTranscriptionOptions;
import org.springframework.ai.openai.api.OpenAiAudioApi;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.client.RestClient;

import java.time.Duration;

@Configuration
public class AudioAiConfig {

    @Value("${musicCoverText.apiKey}")
    private  String API_KEY;
    @Value("${musicCoverText.baseUrl}")
    private String BASE_URL;
    @Value("${musicCoverText.model}")
    private  String MODEL_NAME ;


    @Bean
    @Primary
    public OpenAiAudioApi openAiAudioApi() {
        return OpenAiAudioApi.builder()
                .apiKey(new SimpleApiKey(API_KEY))
                .baseUrl(BASE_URL)
                .restClientBuilder(RestClient.builder()
                        .requestInterceptor((request, body, execution) -> {
                            System.out.println("Request URL: " + request.getURI());
                            System.out.println("Request Headers: " + request.getHeaders());
                            return execution.execute(request, body);
                        }))
                .build();
    }

    @Bean
    @Primary
    public OpenAiAudioTranscriptionOptions transcriptionOptions() {
        return OpenAiAudioTranscriptionOptions.builder()
                .model(MODEL_NAME)
                .language("zh")
                .temperature(0f)
                .responseFormat(OpenAiAudioApi.TranscriptResponseFormat.TEXT)
                .build();
    }

    @Bean
    @Primary
    public OpenAiAudioTranscriptionModel transcriptionModel(
            OpenAiAudioTranscriptionOptions options,
            OpenAiAudioApi openAiAudioApi) {
        return new OpenAiAudioTranscriptionModel(openAiAudioApi, options);
    }
}
